package top.maof.book.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import top.maof.book.result.Result;

@ControllerAdvice
@Slf4j
public class MyExceptionHandler {


    @ExceptionHandler(value = {ServiceException.class})
    @ResponseBody
    public Result serviceExceptionHandler(ServiceException e) {
        return Result.response(e.getStatus(), e.getMessage());
    }


    @ExceptionHandler(value = {Exception.class})
    @ResponseBody
    public Result exceptionHandler(Exception e) {
        String exceptionMessage;
        e.printStackTrace();
        exceptionMessage = e.getMessage();
        log.info("异常:" + e.getMessage());
        return Result.response(500, exceptionMessage);
    }
}
